
import { DefaultLayout, GraphNode } from "@/js/components/GraphNode"
import { GraphView } from "@/js/components/GraphView"
import * as InputLinkNode from "@/js/components/InputLinkNode"
import { LinearLayout } from "@/js/components/Layout"

export class IfElse extends GraphNode{
    layout?: DefaultLayout
    load(){
        super.load()

        const layout = new this.DefaultLayout(this, this.graphView as GraphView)
        this.layout = layout
        this.appendChild(layout.container)

        const nodes: (InputLinkNodeOptions & { layout: 'left' | 'right', inputLinkNode: InputLinkNodes }) []   = [
            {
                name: "condition",
                desc: '条件',
                model: "entry",
                layout: "left",
                inputLinkNode: "InputLinkNodeBoolean"
            },
            {
                name: "true", 
                desc: '真',
                model: "output",
                showInput: false,
                layout: 'right',
                inputLinkNode: "InputLinkNodeFlow"
            },
            {
                name: "false",
                desc: '假', 
                model: "output",
                showInput: false,
                layout: "right",
                inputLinkNode: 'InputLinkNodeFlow'
            }
        ]

        nodes.forEach(node=>{
            if(layout.main){
                const layout_ = layout.main[node.layout]
                if(layout_) layout_.appendChild(
                    new (InputLinkNode as any)[ node.inputLinkNode ]( this, this.graphView,  node )
                )
            }
            
        })
    }
}

IfElse.parser = ({node, toNode, })=>{
    const next = toNode(node.outputs.output[0], 'enter')
    const trueBody = toNode(node.outputs.true[0], 'enter')
    const falseBody = toNode(node.outputs.false[0], 'enter')
    const condition = toNode(node.enters.condition[0], 'output')

    return `if(${condition || node.userData.condition || node.userData.enters.condition || 'false'}){${trueBody}}else{${falseBody}}${next}`
}